package edu.uj.petri.model;

import edu.uj.petri.commands.Memento;

/**
 * @author Piotrek
 */
public class InhibitorArc extends Arc {
    private InhibitorArcImpl impl;
    
    InhibitorArc(Node from, Node to, InhibitorArcImpl impl) {
        super(from, to, impl.getArcImpl());
        this.impl = impl;
        
        if (from instanceof Place && to instanceof Transition) {
            ((Transition)to).addIncomingArc(this);
        } else {
            throw new IllegalArgumentException("inhibitor arc connects starts in place " +
                    "and ends in transition");
        }
    }
    
    /**
     * simply does not do anything
     */
    public void fire() {
    }
    
    /**
     * returns true when place in arc begin is empty
     */
    public boolean canBeFired() {
        Place from = (Place)getFrom();
        return from.isEmpty();
    }
    
    public Memento getMemento() {
        ArcMemento retval = new ArcMemento();
        retval.implMemento = impl.getMemento();
        retval.arcMemento = super.getMemento();
        return retval;
    }
    
    public void applyMemento(Memento memento) {
        if (!(memento instanceof ArcMemento))
            throw new IllegalArgumentException("invalid memento");
        
        ArcMemento am = (ArcMemento)memento;
        impl.applyMemento(am.implMemento);
        super.applyMemento(am.arcMemento);
    }
    
    private static final long serialVersionUID = -3775897098915630390L;
    
    static class ArcMemento extends Memento {
        private Memento implMemento;
        private Memento arcMemento;
    }
}
